{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "广播是指Numpy在算术运算期间处理不同形状的数组的能力。   \n",
    "对数值的算术运算通常在相应的元素上进行。  \n",
    "如果有两个阵列具有完全相同的形状，则这些操作将被无缝执行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 10,  40,  90, 160])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1,2,3,4])\n",
    "b = np.array([10,20,30,40])\n",
    "c = a * b\n",
    "c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果两个数组的维数不同，则元素到元素的操作是不可能的。  \n",
    "然而，在Numpy中仍然可以对形状不相似的数组进行操作，因为它具有**广播功能**。  \n",
    "较小的数组会**广播**到较大的数组的大小，以使它们的形状可兼容。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  0.,  0.,  0.],\n",
       "       [10., 10., 10., 10.],\n",
       "       [20., 20., 20., 20.],\n",
       "       [30., 30., 30., 30.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "a1 = np.array([[0.0,0.0,0.0,0.0],[10.0,10.0,10.0,10.0],[20.0,20.0,20.0,20.0],[30.0,30.0,30.0,30.0]])\n",
    "b1 = np.array([1.0,2.0,3.0,4.0])\n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 2., 3., 4.])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  2.,  3.,  4.],\n",
       "       [11., 12., 13., 14.],\n",
       "       [21., 22., 23., 24.],\n",
       "       [31., 32., 33., 34.]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# b1广播到a1\n",
    "a1+b1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  0.,   0.,   0.,   0.],\n",
       "       [ 10.,  20.,  30.,  40.],\n",
       "       [ 20.,  40.,  60.,  80.],\n",
       "       [ 30.,  60.,  90., 120.]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1*b1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面的图片展示了数组`b`如何通过广播来与数组`a`兼容：  \n",
    "![avatar](http://www.yiibai.com/uploads/images/201704/2404/689090455_30273.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
